#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>


#define RUNNING_DIR	"/tmp"
#define LOCK_FILE	"/tmp/gps_srvd.lock"
#define LOG_FILE	"/tmp/gps_srvd.log"

int main(int argc, char *argv[])
{
    if (argc > 1) {
        int fd, len;
        pid_t pid;
        char pid_buf[16];
        if ((fd = open(LOCK_FILE, O_RDONLY)) < 0)
        {
            printf(LOCK_FILE);
            printf(": Lock file not found. May be the server is not running?\n");
            exit(fd);
        }
        len = read(fd, pid_buf, 16);
        pid_buf[len] = 0;
        pid = atoi(pid_buf);
        if(!strcmp(argv[1], "stop")) 
        {
            kill(pid, SIGTERM);
            exit(EXIT_SUCCESS);
        }
        if(!strcmp(argv[1], "status")) 
        {
            //kill(pid, SIGHUP);
            printf("Server process PID: %d\n", pid);
            exit(EXIT_SUCCESS);
        }
        printf ("usage %s [stop|status]\n", argv[0]);
        exit (EXIT_FAILURE);
    }
    return 0;
}
